# -*- coding: utf-8 -*-

from flask import render_template, g, request

from loremipsum import get_sentence, get_paragraph, \
                       generate_sentences, generate_paragraphs

from itertools import izip
from random import randint
from werkzeug.urls import  Href
from datetime import datetime


def root_page():
    """ """
    if g.user:
        lentas = [{'id': 100500,
                   'namespace': "ebanashka",
                   'name': get_sentence(),
                   'desc': get_paragraph()} for _ in xrange(8)]
        
        return render_template('lenta/root_logged.html', my_lentas=lentas)
    else:
        return render_template('lenta/root_anonymous.html')


def browse_lenta(namespace, lenta_id):
    N = 10
    titles = (t[2][:100]for t in generate_sentences(N))
    twits = (t[2][:140]for t in generate_paragraphs(N))
    notes = ({'title': tl,
              'twit': tw,
              'namespace': namespace,
              'id': 111,
              'lenta_id': 100500} for tl, tw in izip(titles, twits))
    T = 20
    tags =  ({'name': t[2][:randint(5,20)], 'count': randint(5,100), 'namespace': namespace, 'lenta_id': 100500}
                for t in generate_sentences(T))
    
    href = Href(request.path)
    
    next_page_url = href({'dttm': datetime(2012, 1, 31).strftime('%Y-%m-%d-%H-%M-%S'), 'limit': N})
    prev_page_url = href({'dttm': datetime(2012, 1, 20).strftime('%Y-%m-%d-%H-%M-%S'), 'limit': N, 'back':''})
    
    return render_template('/lenta/browse_lenta.html',
                           notes=notes,
                           next_page_url=next_page_url,
                           prev_page_url=prev_page_url)
    
    
def get_notes_by_tag (namespace, lenta_id, tag_name):
    N = 10
    titles = (t[2][:100]for t in generate_sentences(N))
    twits = (t[2][:140]for t in generate_paragraphs(N))
    notes = ({'title': tl, 'twit': tw, 'namespace': namespace, 'id': 111, 'lenta_id': 100500}
                for tl, tw in izip(titles, twits))
    tag = {
        'name': tag_name,
        'namespace': namespace,
        'lenta_id': lenta_id,
        'counter': 12
    }
    return  render_template('/lenta/note/notes_by_tag.html', notes=notes, tag=tag)    